package ua.eug.robov2.schematic.wire;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import com.badlogic.gdx.utils.Array;

import ua.eug.robov2.schematic.chips.Pin;

public class Wire implements Externalizable {
	public Pin inp;
	Array<Pin> outp = new Array<Pin>(false, 4);
	
	public Wire() {
		inp = Pin.Float;
	}
	
	public Wire(Pin inp) {
		this.inp = inp;
	}
	
	public void act() {
		for (Pin p : outp) {
			p.state = inp.state;
		}
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		inp = (Pin) in.readObject();
		int cnt = in.readInt();
		for (int i = 0; i< cnt; i++) {
			Pin p = (Pin) in.readObject();
			outp.add(p);
		}
	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		// TODO keep input pin or float
		out.writeObject(inp);
		out.writeInt(outp.size);
		for (Pin o : outp) {
			out.writeObject(o);
		}
	}
}
